from ctypes import windll, byref
from ctypes.wintypes import RECT
from sys import exc_info
import cv2, dxcam
import numpy as np
import pyautogui
from time import sleep, time, localtime, strftime
#from PIL import Image
from findimage import find_template, find_all_template

# 特殊作用坐标
coords_spec={"map":(0.876,0.185), "vip":(0.2,0.26), "checkin":[0.52, 0.82], "hand":(0.97,0.502), "pack":(0.97,0.415), "off":(0.98,0.117), "recycle1":(0.875, 0.925), "recycle2":(0.85, 0.887), "red":(0.452, 0.132), "active":(0.775,0.035), "linesw":(0.95,0.97)}

def loc_click(hWnd, coord, t = 1):
	rect = RECT()
	windll.user32.GetWindowRect(hWnd, byref(rect))
	# 前台鼠标点击
	width = rect.right - rect.left
	height = rect.bottom - rect.top
	if coord[0] < 1: # 比例
		x, y = int(coord[0]*width), int(coord[1]*height)  # 坐标换算
	else: # 直接坐标
		x, y = coord[0], coord[1]
	pyautogui.moveTo(rect.left + x, rect.top + y) # cody怪物坐标修正
	sleep(0.5)
	pyautogui.click()
	sleep(t)

# 定义鼠标事件的参数
MK_LBUTTON = 1 # 鼠标左键, 右键为2
MOUSE_MOVE = 512 #0x0001  # 鼠标移动
MOUSE_LEFTDOWN = 513 #0x0002  # 左键按下
MOUSE_LEFTUP = 514 #0x0004  # 左键释放
def bkg_click(hWnd, coord, drag = False, delta = 0, count = 1):
	from win32.win32gui import SendMessage
	if coord[0] < 1: # 比例
		x, y = int(coord[0]*864), int(coord[1]*530)  # 坐标换算
	else: # 直接坐标
		x, y = int(coord[0]), int(coord[1])
	while count > 0:
		lParam = x | y <<16
		SendMessage(hWnd, MOUSE_LEFTDOWN, MK_LBUTTON, lParam)
		if drag:
			lParam = x | (y + delta) <<16
			SendMessage(hWnd,MOUSE_MOVE, MK_LBUTTON, lParam)
		SendMessage(hWnd, MOUSE_LEFTUP, None, lParam)
		sleep(0.3)
		count -= 1
	
lines = [(510,175), (510,245), (510,320), (510,392)]
def line_switch(hWnd, line):
	global lines
	bkg_click(hWnd, coords_spec["linesw"])
	sleep(0.5)
	bkg_click(hWnd, (500, 370), True, -90) #拖动
	sleep(0.5)
	line = 0 if line > 4 else (line - 1)
	bkg_click(hWnd, lines[2])
	sleep(0.2)
	bkg_click(hWnd, lines[line])
	sleep(0.5)
	bkg_click(hWnd, (427,427))
	sleep(0.5)

from win32.win32print import GetDeviceCaps
from win32.win32gui import SetWindowPos, GetDC
screen0_w = GetDeviceCaps(GetDC(0),118)
def loc_capture(hWnd, monitor = 0, top = False):
	rect = RECT()
	windll.user32.GetWindowRect(hWnd, byref(rect))
	# 窗口截图
	if monitor == 0: #主屏
		camera = dxcam.create(output_idx=monitor, output_color="BGR")
	else: #第二屏
		camera = dxcam.create(output_idx=1, output_color="BGR")
	if top: # 置顶
		SetWindowPos(hWnd, 0, 0, 0, 0, 0, 2 | 1) 
	else:
		SetWindowPos(hWnd, 1, 0, 0, 0, 0, 2 | 1) 
	image = None; n = 3 # 有失败可能，三次尝试
	while image is None and n > 0: 
		width = screen0_w if monitor == 0 else 1280
		image = camera.grab(region=(max(0,rect.left-monitor),rect.top,min(width, rect.right-monitor),rect.bottom)) #第二屏修正
		n -= 1
	if top: # 置顶
		sleep(0.5)
		SetWindowPos(hWnd, 1, 0, 0, 0, 0, 2 | 1) # 下沉窗口
	return image

player_img = cv2.imread('./Pics/MU_player.png') #, cv2.IMREAD_UNCHANGED)
def getPlayer(image):
	global player_img
	# 获取玩家坐标
	thresh1 = np.array([90, 180, 85], dtype = "uint8")  # 目标颜色阈值
	thresh2 = np.array([100, 200, 105], dtype = "uint8") 
	img_mask = cv2.inRange(image, thresh1, thresh2)
	player_mask = cv2.inRange(player_img, thresh1, thresh2)
	w, h = player_img.shape[:-1]
	for rota in range(0,360,45):
		M = cv2.getRotationMatrix2D((w/2,h/2),rota,1)
		player_loc = cv2.warpAffine(player_mask,M,(w,h))
		player_des = find_template(img_mask, player_loc)
		if player_des and player_des["confidence"] >0.8:
			return player_des
		else:
			continue
	if player_des is None: return False

hand_img = cv2.imread('./Pics/MU_hand.png')
act_img = cv2.imread('./Pics/MU_act.png')
qiang_img = cv2.imread('./Pics/MU_qiang.png')	
def handAct(hWnd, monitor, top = True): # 打怪状态判断
	# 返回值(怪, 抢, 手)
	global hand_img, act_img
	image = loc_capture(hWnd, monitor, top)
	hand_status = find_template(image[267:283, 830:842], hand_img)
	act_status = find_template(image, act_img)
	qiang_status = find_template(image, qiang_img) # 抢归属判断
	#print((hand_status is None) or (hand_status["confidence"]<0.8), ((act_status is not None) and  act_status["confidence"]>0.7) ,((qiang_status is None) or qiang_status["confidence"]<0.7))
	#if hand_status is not None: print(hand_status["confidence"])
	red_dis = np.linalg.norm(np.array([24,25,196]) - image[72,391], ord=1)
	return ((((act_status is not None) and act_status["confidence"]>0.7) or red_dis < 10 ), ((qiang_status is None) or qiang_status["confidence"]<0.7), ((hand_status is None) or hand_status["confidence"]<0.8))

close_img = cv2.imread('./Pics/MU_close.png')
def close_all(hWnd, monitor):
	image = loc_capture(hWnd, monitor)
	close_status = find_template(image[45:115, 80:863], close_img)
	if (close_status is not None) and (close_status["confidence"]>0.7):
		#print(close_status, np.array([80, 45]) + np.array(close_status["result"]))
		#print(f'Close all diag with {close_status["confidence"]}')
		bkg_click(hWnd, tuple(np.array([80, 45]) + np.array(close_status["result"])))

mapls = ['vip', 'uamo1', 'uamo2', 'yivi1', 'fzxu2', 'byud1', 'godu', 'ysve', 'xmzs', 'byfg', 'anny'] 
for map in mapls:
	exec(f'{map}_img = cv2.imread("./Pics/MU_{map}.png")')

def map_check(image):
	map_result = ''
	conf = 0.5
	for i in range(len(mapls)):
		map_status = find_template(image, globals()[mapls[i]+'_img'])
		if map_status is not None and map_status["confidence"] > conf:
#			print(f'map:{mapls[i]}_img, confidence:{map_status["confidence"]}')
			map_result = mapls[i]
			conf = map_status["confidence"]
#	print("map info:", map_result)
	return map_result

map_cls = {'vip':[coords_spec["vip"]], 'yivi1':[(160, 205), (160, 225),[(160, 390), -62]], 'uamo1':[(160, 365),(160, 390)], 'byud1':[(160, 235), (160, 260), [(160, 390), -62]], 'anny':[(160, 215),[(160, 390), -63]], 'godu':[(160, 128),[(160, 390), -120]]}
info = ''
def map_switch(hWnd, amap):
	global info
	info = 'maphold'
	loc_cls = list(map_cls[amap]) #列表复制
	bkg_click(hWnd, (770,110)) # 打开地图
	sleep(2)
#	print(f'length of cls:{len(loc_cls)}')
	if len(loc_cls)>1 and type(loc_cls[-1]) is list:
		bkg_click(hWnd, loc_cls[-1][0], True, loc_cls[-1][1]) # 向上拖动
		sleep(1)
#		del loc_cls[-1]
	bkg_click(hWnd, loc_cls[0])
	sleep(0.3)
	if len(loc_cls) > 1 and type(loc_cls[1]) is tuple:
		bkg_click(hWnd, loc_cls[1])
	if amap == 'vip':
		bkg_click(hWnd, coords_spec["checkin"])
	sleep(1)

login_img = cv2.imread('./Pics/MU_login.png')
start_img = cv2.imread('./Pics/MU_start.png')
relive_img = cv2.imread('./Pics/MU_relive.png')
yao_img = cv2.imread('./Pics/MU_yao.png')
map_img = cv2.imread('./Pics/MU_map.png')
yaoCheck_img = cv2.imread('./Pics/MU_yaoCheck.png')
team_img = cv2.imread('./Pics/MU_team.png')

def diagClose(hWnd, monitor = 0, maphold = 'vip', line = 2, top = False):
	global close_img, vip_img, login_img, yao_img, yaoCheck_img, info
	#cv2.namedWindow('image', 0)
	#cv2.imshow('image',image[43:140, 729:863])
	#cv2.waitKey(0)
	#cv2.destroyAllWindows()
	image = loc_capture(hWnd, monitor, top)
	#try:
	close_status = find_template(image[45:115, 80:], close_img)
	login_status = find_template(image, login_img)
	start_status = find_template(image, start_img)
	relive_status = find_template(image, relive_img)
	team_status = find_template(image, team_img)
	yao_status = find_template(image[480:520, 320:420], yao_img)
	#print(login_status)
	if (team_status is not None) and (team_status["confidence"]>0.7): # 入队申请
		print("team request...")
		bkg_click(hWnd, team_status["result"])
		sleep(1)
		bkg_click(hWnd, (725,445)) # 一键同意
		sleep(1)
		bkg_click(hWnd, (96,110)) # 关闭
	if (login_status is not None) and (login_status["confidence"]>0.7): # 重新登录
		print("relogin after 60s ...")
		sleep(60)
		bkg_click(hWnd, login_status["result"])
		sleep(6)
		bkg_click(hWnd, (430, 462))
		sleep(20)	#资源加载较慢
		bkg_click(hWnd, (89, 105))
		sleep(2)
		bkg_click(hWnd, coords_spec["hand"])
		sleep(2)
	if (start_status is not None) and (start_status["confidence"]>0.7):
		print("Dialog Start_status!")
		bkg_click(hWnd, start_status["result"])
		sleep(10)
		bkg_click(hWnd, (89, 105))
		sleep(2)
		bkg_click(hWnd, coords_spec["hand"])
		sleep(2)
	if (relive_status is not None) and (relive_status["confidence"]>0.7):
		print("Dialog relive_status!")
		info ='relive'
		bkg_click(hWnd, (362,332)) #免费复活
		sleep(2)
		bkg_click(hWnd, (350,320))
		sleep(1)
	if (close_status is not None) and (close_status["confidence"]>0.7):
		#print("Dialog close_all!")
		close_all(hWnd, monitor)
		image = loc_capture(hWnd, monitor, top) #关闭后刷新截图
	if (yao_status is not None) and (yao_status["confidence"]>0.7):
		info = 'yao'
#		print(yao_status["confidence"], yao_status["result"][0])
		if 10 < yao_status["result"][0] <100:
			if yao_status["result"][0] < 50: #需要买红
				bkg_click(hWnd, (350, 500))
				sleep(2) # 点红
				loc_image = loc_capture(hWnd, monitor, top)
				yaoCheck_status = find_template(loc_image, yaoCheck_img)
				if (yaoCheck_status is not None) and yaoCheck_status["confidence"]>0.7:
					bkg_click(hWnd, (366, 340)) # 传送
					sleep(5)
					bkg_click(hWnd, (800, 215), count = 60)
			elif yao_status["result"][0] > 50: #需要买蓝
				bkg_click(hWnd, (400, 500))
				sleep(2) # 点蓝
				loc_image = loc_capture(hWnd, monitor, top)
				yaoCheck_status = find_template(loc_image, yaoCheck_img)
				if (yaoCheck_status is not None) and yaoCheck_status["confidence"]>0.7:
					bkg_click(hWnd, (366, 340)) # 传送
					sleep(5)
					bkg_click(hWnd, (800, 335), count = 60) # 点击90次, 能买到近50组
			bkg_click(hWnd, (606, 62)) # 关闭药店
		close_all(hWnd, monitor)
		map_switch(hWnd, maphold)# 切回原图
		sleep(2)
		line_switch(hWnd, line) #切回原来线路
		sleep(2)
		
def yivi1_Play(window, monitor = 0, top = False):
	from sys import exc_info
	#print(window["title"], window["map"])
	try:
		n = 0
		while True:
			hWnd = window["hWnd"]
			diagClose(hWnd, monitor, window["map"], window["line"], top) # 检测界面异常
			act_stat = handAct(hWnd, monitor, top)
#			SetWindowPos(hWnd, 1, 0, 0, 0, 0, 2 | 1) # 下沉窗口
			if not act_stat[0]:
				map_result = ''
				while map_result != window["map"] :
					close_all(monitor, hWnd)
					bkg_click(hWnd, (770,110)) # 打开地图
					sleep(0.5)
					image = loc_capture(hWnd, monitor, top)
					map_result = map_check(image[89:107, 250:350]) # 地图
					if map_result != window["map"]:
						bkg_click(hWnd, (102,97)) # 关闭地图
						map_switch(hWnd, window["map"]); sleep(2)
						line_switch(hWnd, window["line"]) #切回原来线路
						sleep(2)
						break
				else:
					sleep(2.5) #捡东西
					match_result = find_template(image[209:245,470:497], boss_alive)
					#print("Before:", window["bound"], window["turn"])
					if match_result is not None and match_result["confidence"] > 0.8 and window["bound"][3] == 0:
						window["bound"][:2] = [1,4] # 坐标序列上下限
						window["bound"][3] = 2 # 设置Boss数量
					elif (match_result is None or match_result["confidence"] < 0.8) and window["bound"][3] == 0:
						window["bound"][:2] = [2,3]
					player_des = getPlayer(image)
					if player_des: #根据距离判断序号
						min_dis = 60
						for i in range(len(window["coords"])):
							dis = np.linalg.norm(np.array(window["coords"][i]) - np.array(player_des["result"]), ord=1)
							if dis < min_dis:
								min_dis = dis
								window["turn"] = i
					if window["turn"] == 0:
						window["turn"] = 2
						window["bound"][2] = 1
						bkg_click(hWnd, window["coords"][2])
						sleep(29)
	#					bkg_click(hWnd, (102,97)) # 关闭地图
					elif not window["bound"][0] <= window["turn"] + window["bound"][2] <= window["bound"][1]:
						window["bound"][2] = window["bound"][2]*(-1) #切换方向
					if time() - window["last_time"] > 45: # 换点间隔60s
						window["turn"] = window["turn"] + window["bound"][2]
						tsleep = window["tsleep"][window["turn"]-1] if window["bound"][2] == 1 else window["tsleep"][window["turn"]]
						bkg_click(hWnd, window["coords"][window["turn"]])
	#					print("turn:",window["turn"], "ts:", tsleep, "bound:", window["bound"])
						sleep(tsleep)
						window["last_coord"] = window["coords"][window["turn"]]
						window["last_time"] = time()
						#close_all(hWnd, monitor)
						bkg_click(hWnd, (102,97)) # 关闭地图
					else:
						bkg_click(hWnd, (102,97)) # 关闭地图
						sleep(10)
						continue
					# 开干
					bkg_click(hWnd, coords_spec["hand"])  # 点击手动
					sleep(2)
					if window["bound"][3] > 0 and window["turn"] in [1,4]: window["bound"][3] -= 1
			elif act_stat[0] and not act_stat[2]:
				bkg_click(hWnd, coords_spec["hand"])  # 点击手动
			else:	#降低回收操作频率
				n = n+1 if n<30 else 0
				if n == 30:
					# 回收
					bkg_click(hWnd, coords_spec["pack"]); sleep(2)
					bkg_click(hWnd, (815,485)); sleep(1) # 整理
					bkg_click(hWnd, coords_spec["recycle1"]); sleep(2)
					bkg_click(hWnd, coords_spec["recycle2"]); sleep(2)
					#close_all(hWnd, monitor)
					bkg_click(hWnd, (609,62))
					#sleep(tsleep)
			sleep(5)
	except:
		print("Unexpected error:", exc_info()[0:2])
		pass
		
anny_door = {"right":(524, 335), "left":(443, 419), "top":(440, 336), "bottom":(511, 409)}
def common_Play(window, Boss = False, freq=15, monitor = 0, top = False):
	global info
	try:
		n = 0
		info = ''
		while n < freq:
			hWnd = window["hWnd"]
			diagClose(hWnd, monitor, window["map"], window["line"], top) # 检测界面异常
			if window["map"] == 'anny' and info in ['relive', 'yao', 'maphold']:
				print(strftime("%m-%d %H:%M", localtime()), info)
				info = '' # 重置
				bkg_click(hWnd, (770,110)) # 打开地图
				sleep(0.5)
				map_result = map_check(loc_capture(hWnd, monitor, top)[89:107, 250:350])
				if map_result != window["map"]:
					bkg_click(hWnd, (102,97)) # 关闭地图
					map_switch(hWnd, window["map"])
					bkg_click(hWnd, (770,110)); sleep(0.5)
				bkg_click(hWnd, (759, 390), True, -200) # 向上拖动
				sleep(1)
				bkg_click(hWnd, anny_door[window["annyDoor"]])
				sleep(6)
				bkg_click(hWnd, (102,97)) # 关闭地图
				sleep(0.5)
				bkg_click(hWnd, (510,500)); #续天使
				sleep(0.5)
				bkg_click(hWnd, (770,110)) # 打开地图
				sleep(0.5)
				if window["turn"] == 0:
					window["turn"] = window["bound"][0]
				bkg_click(hWnd, window["coords"][window["turn"]])
				sleep(0.2)
				bkg_click(hWnd, (102,97)) # 关闭地图
				sleep(15)
				bkg_click(hWnd, coords_spec["hand"])  # 点击手动
			act_stat = handAct(hWnd, monitor, top)
			if not act_stat[0]:
				map_result = ''
				while map_result != window["map"] :
					close_all(hWnd, monitor)
					bkg_click(hWnd, (770,110)) # 打开地图
					sleep(0.5)
					image = loc_capture(hWnd, monitor, top)
					map_result = map_check(image[89:107, 250:350]) # 地图
					if map_result != window["map"]:
						bkg_click(hWnd, (102,97)) # 关闭地图
						map_switch(hWnd, window["map"]); sleep(2)
						line_switch(hWnd, window["line"]) #切回原来线路
						sleep(2)
						break
				else:
					sleep(2.5) #捡东西
					if Boss: #红怪
						match_result = find_template(image[273:305, 329:347], boss_alive)
						#print("Before:", window["bound"], window["turn"])
						if match_result is not None and match_result["confidence"] > 0.8:
							window["bound"][0] = 0 # 坐标序列上下限
							window["turn"] = window["bound"][1]
					if time() - window["last_time"] > 15: # 换点间隔15s
						ts_loc = window["tsleep"][window["turn"]]
						if window["turn"] == window["bound"][1]: #头尾衔接
							window["turn"] = window["bound"][0] - 1
						window["turn"] += 1
						bkg_click(hWnd, window["coords"][window["turn"]])
		#					print("turn:",window["turn"], "ts:", tsleep, "bound:", window["bound"])
						sleep(ts_loc)
						window["last_coord"] = window["coords"][window["turn"]]
						window["last_time"] = time()
						if window["turn"] == 0:
							window["bound"][0] = 1
						#close_all(hWnd, monitor)
					else:
						bkg_click(hWnd, (102,97)) # 关闭地图
						sleep(10)
						continue
					bkg_click(hWnd, (102,97)) # 关闭地图
					# 开干
					bkg_click(hWnd, coords_spec["hand"])  # 点击手动
					sleep(2)
			elif act_stat[0] and not act_stat[2]:
				bkg_click(hWnd, coords_spec["hand"])  # 点击手动
			n = n+1
			sleep(5)
		bkg_click(hWnd, (510,500)); sleep(1) #续天使
		# 回收
		bkg_click(hWnd, coords_spec["pack"]); sleep(2)
		bkg_click(hWnd, (815,485)); sleep(1) # 整理
		bkg_click(hWnd, coords_spec["recycle1"]); sleep(2)
		bkg_click(hWnd, coords_spec["recycle2"]); sleep(2)
		#close_all(hWnd, monitor)
		bkg_click(hWnd, (609,62))
		#sleep(tsleep)
		sleep(5)
	except KeyboardInterrupt:
		print(strftime("%m-%d %H:%M", localtime()), "sleep 30s.")
		sleep(30)
		print('...') # going on
		pass
	except:
		print("Unexpected error:", exc_info()[0:2])
		pass

boss_alive = cv2.imread('./Pics/MU_vipBoss.png')
king_alive = cv2.imread('./Pics/MU_king.png')
def bossls(des, image, black_ls=[], king_only = False):
	global boss_alive, king_alive
	h, w = image.shape[:-1] # 取得截图宽度和高度
	if des[0] < 1: # 比例
		zero_point = np.array([int(des[0]*w), int(des[1]*h)])  # 坐标换算
	else: # 直接坐标
		zero_point = np.array(des) # 距离计量起点
	#print(f'截图宽度:{w},高:{h}')
	center_point = np.array([491, 264]) # 坐标旋转圆心
	coords = []
	theta = np.radians(45) #角度转弧度
	#rotation_matrix = np.array([[np.cos(theta), np.sin(theta)], [-np.sin(theta), np.cos(theta)]]) #逆时针
	rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]) #顺时针
	# 获取存活的boss和黄金怪坐标
	if not king_only:
		match_results = find_all_template(image, boss_alive, 0.7, 12)
		for match_result in match_results:
			original_point = np.array(match_result["result"])
			rotated_point = np.dot(rotation_matrix, original_point - center_point) + center_point
			if ((rotated_point[1]/h < 0.77) and 0.45 < rotated_point[0]/w < 0.66):
				x = (match_result["result"][0]) if rotated_point[0] > 490 else (match_result["result"][0]+5)
				y = (match_result["result"][1]-2) if rotated_point[1] > 260 else (match_result["result"][1]+2)
				coords += [((x,y), np.linalg.norm(original_point - zero_point, ord=1))]
			# 角落一个
			elif np.linalg.norm(original_point - np.array([305, 242]), ord=1) < 10:
				coords += [((match_result["result"][0] + 3 ,match_result["result"][1] + 2), 1.7 * np.linalg.norm(original_point - zero_point, ord=1))]
		if len(black_ls) > 0 and len(coords) > 0:
			for black_coord in black_ls:
				for i in range(len(coords)):
					if np.linalg.norm(np.array(black_coord) - np.array(coords[i][0]), ord=1) < 10:
#						print(f'del {i}:{np.array(coords[i][0])}')
						del coords[i]
						break
		coords = sorted(coords, key=lambda x: x[1]) # 按距离排序
		if len(coords) > 1:
			return coords
		elif len(coords) > 0: # 有boss
			match_results = find_all_template(image, king_alive, 0.8, 24)
			for match_result in match_results:
				original_point = np.array(match_result["result"])
				if len(black_ls) > 0:
					dis = 20
					for black_coord in black_ls:
						dis = min(dis, np.linalg.norm(np.array(black_coord) - original_point, ord=1))
					if dis < 10: continue
				rotated_point = np.dot(rotation_matrix, original_point - center_point) + center_point
				if ((rotated_point[1]/h < 0.77) and 0.45 < rotated_point[0]/w < 0.66):
					#x = match_result["result"][0] #-3) if rotated_point[0] > 470 else (match_result["result"][0] + 3)
					#y = match_result["result"][1] #+3) if rotated_point[1] > 135 else (match_result["result"][1] -3)
					distance = np.linalg.norm(original_point - zero_point, ord=1)
					for i in range(len(coords)):
						if 2*distance < coords[i][1]:
							coords.insert(i,(match_result["result"], distance))
							break
						elif i == len(coords):
							coords += [(match_result["result"], distance)]
							break
						else:
							continue
#	else: # 无boss
	match_results = find_all_template(image, king_alive, 0.8, 24)
	for match_result in match_results:
		original_point = np.array(match_result["result"])
		rotated_point = np.dot(rotation_matrix, original_point - center_point) + center_point
		if ((rotated_point[1]/h < 0.77) and 0.45 < rotated_point[0]/w < 0.66):
			#x = match_result["result"][0] #-3) if rotated_point[0] > 470 else (match_result["result"][0] + 3)
			#y = match_result["result"][1] #+3) if rotated_point[1] > 135 else (match_result["result"][1] -3)
			distance = np.linalg.norm(original_point - zero_point, ord=1)
			coords += [(match_result["result"], distance)]
	if len(black_ls)>0 and len(coords) > 0:
		for black_coord in black_ls:
			for i in range(len(coords)):
				if np.linalg.norm(np.array(black_coord) - np.array(coords[i][0]), ord=1) < 10:
					del coords[i]
					break
	coords = sorted(coords, key=lambda x: x[1]) # 按距离排序
	#print(coords)
	#print(f"找到{len(coords)}个怪!")
	# 排序
	if len(coords) > 0:
		return coords
	else:
		return False
